<?php
/**
 * 官方快三开奖
 */
include_once 'common.php';
include_once (BASE_PATH . '/module/lottery/script_common.inc.php');
echo '<pre>';
// 计时变量
$btime       = time();

$lottTab     = $dbprefix . 'lottery';
$lottCateTab = $dbprefix . 'lottery_category';
$qihaoTab    = $dbprefix . 'lottery_k3';
// 获取快三所有的彩种信息
$sql         = 'SELECT lott.id, lott.`sname`, lott.`title`, lott.`ptid`, lott.`credit`
                FROM ' . $lottTab . ' AS lott
                LEFT JOIN ' . $lottCateTab . ' AS cate
                ON lott.`cate_id` = cate.`id`
                WHERE credit = 0';
$list         = getList($sql, $db);

$k3Day        = date('Ymd');
$maxRow       = 20;
$openTime     = time();
foreach ($list as $k => $v){
    // 期号补充前缀20
    $prefix   = '20';
    $openCode = '';
    $target   = '';
    $getUrl   = '';
    $sname    = $v['sname'];
    $qihao_transformation = false;
    switch ($v['sname']){
        // 快三=============================================
        case 'hubk3':
            $sname     = 'hbk3';
            $openCode  = catchK3_1($sname, $maxRow, $prefix);
            break;
        case 'shk3':
            // 上海快三
            $sname     = 'shhk3';
            $qihao_transformation = $qihao_transformation ? $qihao_transformation : true;
            $prefix    = '';
            $openCode  = catchK3_1($sname, $maxRow, $prefix);
            // 获取到的期号需要处理
            foreach ($openCode as $key => $value){
                // 上海快3， 原开奖期号：20180416-09 改为  20180416009（短横杠改为0），上海快3每天期数为82期， 不会出现百位数， 直接替换短横杠为0补位
                $openCode[$key]['expect'] = str_replace('-', '0', $value['expect']);
            }
            break;
        case 'jsk3':
            $qihao_transformation = $qihao_transformation ? $qihao_transformation : true;
            $openCode  = catchK3_1($sname, $maxRow, $prefix);
            foreach ($openCode as $key => $value){
                // 江苏快三， 获取的开奖期号：2018041613， 我们的期号：20180416013
                $openCode[$key]['expect'] = date('Ymd') . '0' . substr($value['expect'], -2);
            }
            break;
        case 'gzk3':
            $prefix    = '';
            $openCode  = catchK3_1($sname, $maxRow, $prefix);
            break;
        case 'bjk3':
            $prefix    = '';
            $maxRow    = '89';
            $openCode  = catchK3_1($sname, $maxRow, $prefix);
            sort($openCode);
            break;
        case 'hebk3':
        case 'gxk3':
        case 'jlk3':
        case 'ahk3':
        case 'gsk3':
            $openCode  = catchK3_1($sname, $maxRow, $prefix);
            break;
        // 十一选五===========================================
        case 'gd11x5':
            $openCode = open_syxw_1('gd11', 20);
            break;
        case 'sh11x5':
            $openCode = open_syxw_1('sh11', 20);
            break;
        case 'sd11x5':
            $openCode = open_kkcz('sd11x5kj', 5, 2, 20, '20');
            break;
        case 'jx11x5':
            $openCode = open_kkcz('jx11x5kj', 5, 2, 20, '20');
            break;
        // 时时彩===========================================
        case 'cqssc':
            $prefix = '';
            $qihao_transformation = true;
            $openCode = open_kkcz($v['sname'] . 'kj', 9, 1, 20, $prefix);
            foreach ($openCode as $key => $value){
                // 重庆时时彩，采集回来结果中期号为 180416050，系统中村的期号为 180416-077
                $openCode[$key]['expect'] = substr($value['expect'], 0, 6) . '' . substr($value['expect'], -3);
            }
            break;
        case 'xjssc':
            $openCode = open_xjssc(20);
            foreach ($openCode as $key => $value){
                // 新疆时时彩，采集回来结果中期号为 20180416026，系统中村的期号为 2018041696
                $openCode[$key]['expect'] = substr($value['expect'], 2, 6) . substr($value['expect'], -3);
            }
            break;
        case 'tjssc':
            $prefix   = '';
            $openCode = open_kkcz($v['sname'] . 'kj', 9, 1, 20, $prefix);
            break;
    }
    
    foreach ($openCode as $ck => $cv){
        if ($sname == 'bjk3'){
            // 检查当前彩种期号是否有开奖
            $checkFlagSql = 'SELECT id, `code`, period FROM ' . $qihaoTab . ' WHERE `day` = ' . $k3Day . ' AND tid = ' . $v['id'] . ' ORDER BY id ASC LIMIT ' . $ck . ',1';
            $checkInfo    = getList($checkFlagSql, $db, 1);
            // 有开奖记录，则跳过
            if($checkInfo['code'] != '' || !$checkInfo['id']) continue;
        
        }else{
            // 检查当前彩种期号是否有开奖
            $checkFlagSql = 'SELECT id, `code` FROM ' . $qihaoTab . ' WHERE tid = "' . $v['id'] . '" AND period = "' . $cv['expect'] . '"';
            $checkInfo    = getList($checkFlagSql, $db, 1);
            // 有开奖记录，则跳过
            if($checkInfo['code'] != '' || !$checkInfo['id']) continue;
        }
        // 更新当前彩种期号的开奖结果
        $sql  = 'UPDATE ' . $qihaoTab . ' SET ' . CreateUpdateSql(array('code' => $cv['opencode'], 'opentime' => $openTime, 'status' => '1')) . ' WHERE id = ' . $checkInfo['id'];
        echo $sname . ' - ' . $ck;
//         print_r($cv);
//         echo $sql . '<br/>';
        $flag = $db->exec($sql);
        if ($flag){
            echo '彩种:' . $sname . ' 期号:' . $cv['expect'] . ' 开奖成功';
        }else{
            echo '开奖写入失败！';
        }
        
        echo '<br/>';
    }
}
echo 'end，耗时【' . (time() - $btime) . '】';

